const app = getApp()
var upload = require('../../../utils/upload');
var area = require('../../../utils/area');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    areaList:area.default,
    latitude:'',
    longitude:'',
    fileList: [],
    tempFilePaths: [],
    tempFileNum:0,
    address:'',
  },
  //打开载入
  onLoad: function (options) {
    app.getParam(options);
  },
  onShow: function () {
    app.loginState((rel)=>{
      if(rel){
        if(app.globalData.appConfig.store.is_store){
          app.wxAlert('已是商家身份',()=>{
           this.onClickLeft();
          })
        }
      }else{
        app.wxAlert('你还没有登录',()=>{
          this.onClickLeft();
        })
      }
    },false)
  },
  /**
   * 提交绑定
   */
  formSubmit: function (e) {
    var data = e.detail.value;
    if (app.util().isNull(data.city)) {
      app.wxAlert('城市必须选择');
    }else if (app.util().isNull(data.name)) {
      app.wxAlert('好店名称必须填写');
    }else if (app.util().isNull(data.address)) {
      app.wxAlert('好店地址必须填写');
    } else if (app.util().isNull(data.telphone)) {
      app.wxAlert('联系电话必须填写');
    } else if (app.util().isNull(data.address)) {
      app.wxAlert('引客让利比例必须填写');
    } else if (app.util().isNull(this.data.tempFilePaths)) {
      app.wxAlert('商家Logo必须填写');
    }else {
      this.setData({
        disabled:true
      })
      this.postFile((file) => {
        if (file.isend == true){
          app.api().Post('api/v1/ais/store/reg', {
            name: data.name,
            address: data.address,
            charge: data.charge,
            telphone: data.telphone,
            latitude:  this.data.latitude,
            longitude: this.data.longitude,
            citypath: JSON.stringify(this.data.citypath),
            imgs: JSON.stringify(file.filePaths)
          },(res)=>{
            this.setData({
              disabled:false
            })
            app.wxAlert(res.msg,()=>{
              wx.redirectTo({
                url: '../mch/reg',
              })
            });
          })
          setTimeout(()=>{
            this.setData({
              disabled:false
            })
          },1500)
        }
      })
    }
  },
  //图片上传
  postFile: function (callback) {
    upload.Upfiles('api/v1/ais/config/upimg', this.data.tempFilePaths,callback);
  },
  //上传选择图片
  afterRead:function(event){
    const { file } = event.detail;
    var fileList = this.data.fileList;
    var tempFilePaths = this.data.tempFilePaths;
    fileList.push({url: file.path});
    tempFilePaths.push(file.path);
    this.setData({
      fileList:fileList,
    });
    this.data.tempFilePaths = tempFilePaths;
  },
  //删除图片
  deleteItem: function (res) {
    var tempFilePaths = this.data.tempFilePaths;
    var fileList = this.data.fileList;
    var index = res.target.id;
    if (index > -1) {
      fileList.splice(index,1);
      tempFilePaths.splice(index, 1);
    }
    this.setData({
      fileList:fileList
    })
    this.data.tempFilePaths = tempFilePaths;
  },
  getMapsAddress: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          address: res.address,
          latitude:res.latitude,
          longitude:res.longitude
        })
      },
      fail: function () {
        wx.getSetting({
          success: function (res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                success: function (tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({title: '授权成功',icon: 'success',duration: 1000})
                          //授权成功之后，再调用chooseLocation选择地方
                          wx.chooseLocation({
                            success: function (res) {
                              that.setData({
                                address: res.address,
                                latitude:res.latitude,
                                longitude:res.longitude
                              })
                            },
                          })
                        } else {
                          wx.showToast({title: '授权失败',icon: 'success',duration: 1000})
                        }
                      }
                    })
                  }
                }
              })
            }
          },
          fail: function (res) {
            wx.showToast({title: '调用授权窗口失败',icon: 'success',duration: 1000})
          }
        })
      }
    })
  },
  //会员制度
  onRules: function (e) {
    var config = wx.getStorageSync('config');
    app.webview(app.apiroot+'/app/'+config.app_id+'/ais/article/sla');
  },
  //弹窗
  onClose: function (e) {
    this.setData({
      show:!this.data.show
    })
  },
  onArea: function (e) {
    this.onClose();
    this.setData({
      city:e.detail.values[0].name+e.detail.values[1].name+e.detail.values[2].name
    })
    this.data.citypath = e.detail.values;
  },
  //返回页面
  onClickLeft() {
    var pages = getCurrentPages();
    if(pages.length == 2){
      wx.navigateBack({
        delta:1,
      })
    }else{
      wx.switchTab({
        url: '/tab/vip',
      })
    }
  }
})